(function() {
    'use strict';

    /**
     * 各种常用的方法存储在这里
     */
    angular.module('webApp')

    .factory('helper', ['$timeout', '$filter', 'md5','$sce', function($timeout, $filter, md5,$sce) {
        this.px2pt = function(px) {
            return px * 1;
        }


        /*****************
        页面右下角显示消息
        ******************/
        this.message = function(msg, type, wait) {
            var mid = md5.createHash(msg);
            if (angular.element("#" + mid).length > 0) return;
            $timeout(function() {
                // type 可选值：sussess  error
                wait = _.isNumber(wait) ? wait : 3;
                // standard notification
                // setting the wait property to 0 will
                // keep the log message until it's clicked
                alertify.log("<span id='" + mid + "'>" + msg + "</span>", type, wait * 1000);
            });
        }

        /*****************
        在元素上面显示消息
        <button ng-click="abc(a,$event);">保存</button>

        $scope.acb(a,$event){
            //.......
            helper.myTip("更新成功！", $event.currentTarget);
        }
        ******************/

        this.myTip = function(msg, elm, url, timeout, cb, po, style) {
            console.log("222222");
            $timeout(function() {
            //     // console.log("------",elm)
                timeout = timeout || 2;
            //     po = po || 'top';
            //     var pp = {
            //         top: "hint--top",
            //         right: "hint--right",
            //         bottom: "hint--bottom",
            //         left: "hint--left",
            //     }

                // var tp = {
                //     error: "hint--error",
                //     warning: "hint--warning",
                //     info: "hint--info",
                //     success: "hint--success",
                // }
                // var tt = " " + tp[style] || "";
               $.toptip( $filter('translate')(msg), style);

            //     var hintClass = "hint--always hint--bounce " + pp[po] + tt;
            //     $(elm).addClass(hintClass);

            //     $(elm).attr("aria-label", $filter('translate')(msg)) ;
            //     $timeout(function() {
            //         $(elm).removeClass(hintClass);
            //         if (url) {
            //             location.href = url;
            //         }
            //         if (cb) {
            //             cb();
            //         }
            //     }, timeout * 1000);
            });
        }

        this.myTip2 = function(msg, elm, url, timeout, cb, po) {
            var distroy = false;
            timeout = timeout || 2;
            po = po || 'top';

            $(elm).tooltip({
                animation: false,
                type: 'default',
                placement: po,
                html:true,
                title: msg,
                 width:'300%',
                delay:{show:400, hide: 3000},
                hide: function() {
                    if (!distroy) {
                        distroy = true;
                        $(elm).tooltip('destroy');
                        if (url) {
                            location.href = url;
                        }
                        if (cb) {
                            cb();
                        }
                    }
                }
            }).tooltip('show');
        }

        /*****************
        消息弹窗
        msg: string 弹窗中显示的文字
        url: 窗口关闭后切换到url
        timeout: 消息延时多少秒自动关闭窗口
        cb: 窗口关闭回调函数
        ******************/
        this.myAlert = function(msg, url, timeout, cb) {
            timeout = timeout || 3;
            var dialog= new auiDialog();
            console.log(dialog);
            dialog.alert({title:"",
                msg:$filter('translate')(msg),
                 buttons:[$filter('translate')('close')],
            },function(ret){
                dialog.close();
                if (url) {
                        location.href = url;
                    }
                    if (cb)
                        cb();
            });
             timeMsg();

            function timeMsg() {
                // console.log("倒计时：", timeout);
                setTimeout(function() {
                    $("#myAlertCloseBtn").html('（' + timeout + 's）');
                    if (timeout > 0) {
                        timeMsg();
                    }
                }, 1000);
                timeout -= 1;
            }

            $timeout(function() {
                if(document.querySelector(".aui-dialog")&&document.querySelector(".aui-mask")){
                    dialog.close();
                }
               
                if (url) {
                    location.href = url;
                }
                if (cb) {
                    cb();
                }
            }, timeout * 1000);
        }
        //Url为确认跳转路由url2为取消路由根据需要设定
         this.myAlert2 = function(msg, url,url2) {
            var dialog= new auiDialog();
            dialog.alert({title:"",
                msg:$filter('translate')(msg),
                 buttons:[$filter('translate')('cancel'),$filter('translate')('ok')],
            },function(ret){

                dialog.close();
                if(ret.buttonIndex==1){
                    if (url2) {
                        location.href=url2;
                    }
                }else if(ret.buttonIndex==2){
                    if(url){
                        location.href = url;
                    }
                }
                
                    
            });
        }
        this.toast=function(msg,status,cb){
            var toast=new auiToast();
            if(status=="success"){
                toast.success({
                    title:msg,
                    duration:2000
                });
            }else if(status=="error"){
                toast.fail({
                    title:msg,
                    duration:2000
                });
            }else if(status=="loading"){
                toast.loading({
                    title:msg,
                    duration:2000
                },function(ret){
                    cb();
                });
            }else if(status=="text"){
                $.toast(msg,"forbidden")
            }else{
                console.log("322");
                toast.custom({
                    title:msg,
                    html:"",
                    duration:2000
                });
            }

        }
        /*****************
        确认弹窗
        msg: string 弹窗中显示的文字
        okcb: function 确认后的回调函数
        nocb: function 取消后的回调函数
        ******************/
        this.myConfirm = function(msg, okcb, nocb) {
            var m = _.isString(msg) ? msg : msg.msg;
            m = m || "ok_or_no";
            var haveInput = _.isObject(msg) && msg.remark;
            var body = $filter('translate')(m);
            if (haveInput) {
                body += '<br><textarea id="confirmRemark" style="width:98%;height:100px;"></textarea>'
            }

            function remark() {
                if(_.isString(msg)) return '';
                if (!haveInput) return '';
                var r = $("#confirmRemark").val();
                if (!r) {
                    alert($filter('translate')('required'));
                    return false;
                } else {
                    return r;
                }
            }

            $.confirm({
                backdrop: 'static',
                keyboard: true,
                title: '<i class="sui-icon icon-tb-notification"></i>',
                body: body,
                okBtn: $filter('translate')('ok'),
                cancelBtn: $filter('translate')('cancel'),
                closeBtn: true,
                transition: true,
                width: 'small',
                okHide: function() {
                    var rk = remark();
                    if (rk === false) return false;

                    if (okcb) {
                        okcb(rk);
                    }
                    return true;
                },
                cancelHide: function(e) {
                    var rk = remark();
                    if (rk === false) return false;

                    if (nocb) {
                        nocb(rk);
                    }
                    return true;
                }
            });
        }
        return this;
    }]);
})();

//跳转
function gourl(url) {
    location.href = url;
}

/*****************
产生一个随机整数
最大值是max
******************/
function random(max) {
    max = max || 100;
    return Math.floor(Math.random() * max);
}

/*****************
克隆一个对象
obj：任意js对象
******************/
function loadingToggle(f) {
    console.log("111");
    if (f) {
        $("#initloadingdiv").hide();
        $("#bodydiv").show();
        $(".sui-modal-backdrop").removeClass("login-modal");
    } else {
        $("#initloadingdiv").show();
        $("#bodydiv").hide();
        $(".sui-modal-backdrop").addClass("login-modal");
    }
}

/*****************
克隆一个对象
obj：任意js对象
******************/
function clone(obj) {
    if (!obj) return obj;
    return JSON.parse(angular.toJson(obj));
}
